﻿package com.Galaxisfarm {
	// Bibliotecas da game engine Flixel
	import com.adamatomic.flixel.*;
	
	public class StoryState extends GameState {
		// Imagem das bordas da história
		[Embed(source = "../../data/story_border.png")] private var ImgStoryBorder:Class;
		// Imagem do botão de pular
		[Embed(source = "../../data/skip_button.png")] private var ImgSkipButton:Class;
		[Embed(source = "../../data/frame_1_color.png")] private var ImgFrame1Color:Class;
		[Embed(source = "../../data/frame_1_bw.png")] private var ImgFrame1BW:Class;
		[Embed(source = "../../data/frame_2_bw.png")] private var ImgFrame2BW:Class;
		[Embed(source = "../../data/frame_3_bw.png")] private var ImgFrame3BW:Class;
		[Embed(source = "../../data/frame_4_bw.png")] private var ImgFrame4BW:Class;
		[Embed(source = "../../data/frame_5_bw.png")] private var ImgFrame5BW:Class;
		[Embed(source = "../../data/frame_6_bw.png")] private var ImgFrame6BW:Class;
		[Embed(source = "../../data/frame_2_color.png")] private var ImgFrame2Color:Class;
		[Embed(source = "../../data/frame_3_color.png")] private var ImgFrame3Color:Class;
		[Embed(source = "../../data/frame_4_color.png")] private var ImgFrame4Color:Class;
		[Embed(source = "../../data/frame_5_color.png")] private var ImgFrame5Color:Class;
		[Embed(source = "../../data/frame_6_color.png")] private var ImgFrame6Color:Class;
		[Embed(source = "../../data/dialog_frame_2_1.png")] private var ImgDialogFrame21:Class;
		[Embed(source = "../../data/dialog_frame_2_2.png")] private var ImgDialogFrame22:Class;
		[Embed(source = "../../data/dialog_frame_2_3.png")] private var ImgDialogFrame23:Class;
		[Embed(source = "../../data/dialog_frame_3.png")] private var ImgDialogFrame3:Class;
		[Embed(source = "../../data/dialog_frame_4.png")] private var ImgDialogFrame4:Class;
		[Embed(source = "../../data/dialog_frame_5.png")] private var ImgDialogFrame5:Class;
		[Embed(source = "../../data/dialog_frame_5_2.png")] private var ImgDialogFrame52:Class;
		[Embed(source = "../../data/dialog_frame_6.png")] private var ImgDialogFrame6:Class;
		
		// Em qual estado está o jogo atualmente
		private var _state:int = 1;
		private var _step1:int = 0;
		private var _step1Count:Number = 0.3;
		private var _dialogFrame21:FlxSprite;
		private var _dialogFrame22:FlxSprite;
		private var _dialogFrame23:FlxSprite;
		
		private var _dialogFrame3:FlxSprite;
		private var _dialogFrame4:FlxSprite;
		private var _dialogFrame5:FlxSprite;
		private var _dialogFrame52:FlxSprite;
		private var _dialogFrame6:FlxSprite;
		
		private var _frame1Color:FlxSprite;
		private var _frame2Color:FlxSprite;
		private var _frame3Color:FlxSprite;
		private var _frame4Color:FlxSprite;
		private var _frame5Color:FlxSprite;
		private var _frame6Color:FlxSprite;
		private var _frameCount:Number = 2;
		
		// Método construtor
		public function StoryState():void {
			super();
			
			// Adiciona o background no palco
			var backgroundCerrado:BackgroundCerrado = new BackgroundCerrado(20, 20);
			backgroundCerrado.play("normal");
			_lyr_stage.add(backgroundCerrado);
			
			// Adiciona a imagem de bordas
			var storyBorder:FlxSprite = new FlxSprite(ImgStoryBorder, 0, 0);
			_lyr_stage.add(storyBorder);
			
			_lyr_sprites.add(new FlxSprite(ImgFrame1BW, 31, 50));
			_lyr_sprites.add(new FlxSprite(ImgFrame2BW, 331, 50));
			_lyr_sprites.add(new FlxSprite(ImgFrame3BW, 474, 50));
			_lyr_sprites.add(new FlxSprite(ImgFrame4BW, 35, 221));
			_lyr_sprites.add(new FlxSprite(ImgFrame5BW, 233, 221));
			_lyr_sprites.add(new FlxSprite(ImgFrame6BW, 428, 221));
			
			_frame1Color = new FlxSprite(ImgFrame1Color, 31, 50);
			_frame1Color.visible = true;
			_frame2Color = new FlxSprite(ImgFrame2Color, 331, 50);
			_frame2Color.visible = false;
			_frame3Color = new FlxSprite(ImgFrame3Color, 474, 50);
			_frame3Color.visible = false;
			_frame4Color = new FlxSprite(ImgFrame4Color, 35, 221);
			_frame4Color.visible = false;
			_frame5Color = new FlxSprite(ImgFrame5Color, 233, 221);
			_frame5Color.visible = false;
			_frame6Color = new FlxSprite(ImgFrame6Color, 428, 221);
			_frame6Color.visible = false;
			
			_dialogFrame21 = new FlxSprite(ImgDialogFrame21, 326, 25);
			_dialogFrame21.visible = false;
			_dialogFrame22 = new FlxSprite(ImgDialogFrame22, 371, 60);
			_dialogFrame22.visible = false;
			_dialogFrame23 = new FlxSprite(ImgDialogFrame23, 418, 26);
			_dialogFrame23.visible = false;
			
			_dialogFrame3 = new FlxSprite(ImgDialogFrame3, 428, 160);
			_dialogFrame3.alpha = 0;
			
			_dialogFrame4 = new FlxSprite(ImgDialogFrame4, 168, 180);
			_dialogFrame4.alpha = 0;
			
			_dialogFrame5 = new FlxSprite(ImgDialogFrame5, 110, 290);
			_dialogFrame5.alpha = 0;
			
			_dialogFrame52 = new FlxSprite(ImgDialogFrame52, 370, 195);
			_dialogFrame52.alpha = 0;
			
			_dialogFrame6 = new FlxSprite(ImgDialogFrame6, 515, 170);
			_dialogFrame6.alpha = 0;
			
			_lyr_sprites.add(_frame1Color);
			_lyr_sprites.add(_frame2Color);
			_lyr_sprites.add(_frame3Color);
			_lyr_sprites.add(_frame4Color);
			_lyr_sprites.add(_frame5Color);
			_lyr_sprites.add(_frame6Color);
			
			_lyr_sprites.add(_dialogFrame21);
			_lyr_sprites.add(_dialogFrame22);
			_lyr_sprites.add(_dialogFrame23);
			
			_lyr_sprites.add(_dialogFrame3);
			_lyr_sprites.add(_dialogFrame4);
			_lyr_sprites.add(_dialogFrame5);
			_lyr_sprites.add(_dialogFrame52);
			_lyr_sprites.add(_dialogFrame6);
			
			// Botão avançar
			_lyr_sprites.add(new FlxButton(560, 360, new FlxSprite(ImgSkipButton), function():void {
				FlxG.switchState(RegionState);
			}));
			
			addVersionBeta();
		}
		
		override public function update():void {
			super.update();
			
			switch (_state) {
				case 2:
					if (_step1Count < 0) {
						_step1Count = 0.3;
						
						if (_step1 == 0) {
							_dialogFrame21.visible = true;
							_step1++;
						} else if (_step1 == 1) {
							_dialogFrame22.visible = true;
							_step1++;
						} else if (_step1 == 2) {
							_dialogFrame23.visible = true;
							_step1++;
						} else if (_step1 == 3) {
							_dialogFrame21.visible = false;
							_dialogFrame22.visible = false;
							_dialogFrame23.visible = false;
							_step1 = 0;
						}
					} else {
						_step1Count -= FlxG.elapsed;
					}
					break;
				case 3:
					if (_dialogFrame3.alpha < 100) {
						_dialogFrame3.alpha += 0.1;
					}
					break;
				case 4:
					if (_dialogFrame4.alpha < 100) {
						_dialogFrame4.alpha += 0.1;
					}
					break;
				case 5:
					if (_dialogFrame5.alpha < 100) {
						_dialogFrame5.alpha += 0.1;
					}
					if (_dialogFrame52.alpha < 100) {
						_dialogFrame52.alpha += 0.1;
					}
					break;
				case 6:
					if (_dialogFrame6.alpha < 100) {
						_dialogFrame6.alpha += 0.1;
					}
					break;
			}
			
			if (_frameCount < 0) {
				_frameCount = 2;
				
				switch (_state) {
					case 1:
						_frame1Color.visible = false;
						_frame2Color.visible = true;
						break;
					case 2:
						_frame2Color.visible = false;
						_dialogFrame21.visible = false;
						_dialogFrame22.visible = false;
						_dialogFrame23.visible = false;
						
						_frame3Color.visible = true;
						break;
					case 3:
						_frame3Color.visible = false;
						_dialogFrame3.visible = false;
						
						_frame4Color.visible = true;
						break;
					case 4:
						_frame4Color.visible = false;
						_dialogFrame4.visible = false;
						
						_frame5Color.visible = true;
						break;
					case 5:
						_frame5Color.visible = false;
						_dialogFrame5.visible = false;
						_dialogFrame52.visible = false;
						
						_frame6Color.visible = true;
						break;
					case 6:
						_dialogFrame6.visible = false;
					
						FlxG.switchState(RegionState);
						break;
				}
				
				_state++;
			} else {
				_frameCount -= FlxG.elapsed;
			}
		}
	}
}